<?php

class RelativeReviewsController extends AppController {

    var $name = 'RelativeReviews';

    function index($relative_id, $candidate_id) {
        $this->layout = "ajax";
        $this->set('relative_id', $relative_id);
        $this->set('candidate_id', $candidate_id);
        $this->set('revisiones', $this->RelativeReview->find('all', array('conditions' => array('RelativeReview.relative_id' => $relative_id), 'order' => array('RelativeReview.id DESC'), 'fields' => array('RelativeReview.concepto', 'RelativeReview.id', 'RelativeReview.fecha', 'User.id', 'User.nombre', 'User.primer_apellido', 'User.segundo_apellido'))));
    }

    function add($relative_id) {


        if (empty($this->data)) {
            //$this->RelativeReview->Candidate->recursive = -1;
            $this->data = $this->RelativeReview->Relative->find("first", array("conditions" => array("Relative.id" => $relative_id), "fieldfs" => array("Relative.*")));
            $this->data['RelativeReview']['user_id'] = $this->Auth->user('id');
            date_default_timezone_set("America/Bogota");
            $this->data['RelativeReview']['fecha'] = date("Y-m-d-h-i-s");
        } else {
            $this->data['RelativeReview']['user_id'] = $this->Auth->user('id');
            date_default_timezone_set("America/Bogota");
            $this->data['RelativeReview']['fecha'] = date("Y-m-d-h-i-s");
            if ($this->RelativeReview->saveAll($this->data)) {
                $log = $this->RelativeReview->Relative->findLog(array('conditions' => array('Log.action' => 'edit', 'Log.user_id' => $this->Auth->user('id')), 'model' => 'Relative', 'fields' => array('change'), 'order' => 'Log.id DESC ', 'limit' => 1));
                $txt = $log[0]['Log']['change'];
                $this->RelativeReview->UpdateAll(array('RelativeReview.cambio' => "'$txt'"), array('RelativeReview.id' => $this->RelativeReview->getLastInsertID()));
                $this->Session->setFlash('Registro guardado correctamente');
                $this->Session->setFlash('Los datos del aspirante se han guardado con éxito');
                $this->redirect(array('controller' => 'RelativeReviews', 'action' => 'index', $this->data['Relative']['id'], $this->data['Relative']['candidate_id']));
            } else {
                $this->Session->setFlash('Error guardando datos');
            }
        }
    }

}

?>
